<template>
  <div>
    <!-- 上半部分，三级联动 -->
    <el-card style="margin: 20px 0">
      <!-- 三级联动已经注册为了全局组件 -->
      <CategorySelect
        @getCategoryId="getCategoryId"
        :isShow="scene != 0"
      ></CategorySelect>
    </el-card>
    <el-card>
      <!-- 底部有三部分切换 -->
      <!-- 展示SPU列表 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icion-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column
            type="index"
            label="序号"
            width="80"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="spuName"
            label="SPU名称"
            width="width"
          ></el-table-column>
          <el-table-column
            prop="description"
            label="SPU描述"
            width="width"
          ></el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-tooltip content="添加sku" placement="bottom" effect="light">
                <el-button
                  type="success"
                  icon="el-icon-plus"
                  size="mini"
                  @click="addSku(row)"
                >
                </el-button>
              </el-tooltip>
              <el-tooltip content="修改spu" placement="bottom" effect="light">
                <el-button
                  type="warning"
                  icon="el-icon-edit"
                  size="mini"
                  @click="updataSpu(row)"
                >
                </el-button>
              </el-tooltip>

              <el-tooltip
                content="查看当前spu全部sku列表"
                placement="bottom"
                effect="light"
                style="margin: 0 10px"
              >
                <el-button
                  type="info"
                  icon="el-icon-info"
                  size="mini"
                  @click="checkSku(row)"
                >
                </el-button>
              </el-tooltip>
              <!-- <el-popconfirm title="确实要删除吗" @onConfirm="deleteSpu(row)"> -->

              <el-tooltip content="删除spu" placement="bottom" effect="light">
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="deleteSpu(row)"
                >
                  <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    slot="reference"
                  >
                  </el-button>
                </el-popconfirm>
              </el-tooltip>
              <!-- </el-popconfirm> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <!-- 这边点了第几页后，会自动注入参数，若不注入再默认是1 -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :pager-count="7"
          :page-size="limit"
          layout="prev,pager,next,jumper,->,sizes,total"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
          :total="total"
        ></el-pagination>
      </div>
      <!-- 添加||修改SPU -->
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <!-- 添加SKU -->
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScene="changeScene"
      ></SkuForm>
    </el-card>
    <!-- 查看sku的弹出框 -->
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="closeSku"
    >
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column
          prop="skuName"
          label="名称"
          width="width"
        ></el-table-column>
        <el-table-column
          prop="price"
          label="价格"
          width="width"
        ></el-table-column>
        <el-table-column
          prop="weight"
          label="重量"
          width="width"
        ></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{ row }">
            <img :src="row.skuDefaultImg" style="height: 100px; width: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm/index.vue";
import SkuForm from "./SkuForm/index.vue";

export default {
  name: "Spu",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      //控制三级联动是否可以选择
      isShow: true,
      page: 1, //分页器当前第几页
      limit: 5, //每一页要展示多少条数据
      records: [], //存储SPU列表
      total: 0, //分页器一共需要展示数据条数
      scene: 0, //0代表展示SPU列表数据，1代表添加或修改SPU，2代表添加SKU
      //控制对话框的显示与隐藏
      dialogTableVisible: false,
      spu: {},
      skuList: [], //存储SKU的列表数据
      loading: true,
    };
  },
  components: {
    SkuForm,
    SpuForm,
  },
  methods: {
    //自定义事件的回调，把对应的数据展示出来
    getCategoryId({ category1Id, category2Id, category3Id }) {
      if (category1Id && category2Id && category3Id) {
        //发请求展示数据
        this.category1Id = category1Id;
        this.category2Id = category2Id;
        this.category3Id = category3Id;
        this.getSpuList();
      }
    },
    //发请求获取spu列表
    async getSpuList(pages = 1) {
      this.page = pages;
      const { page, limit, category3Id } = this;
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      if (result.code == 200) {
        this.total = result.data.total; //总数据条数
        this.records = result.data.records; //数据列表
      }
    },
    //当分页器第一页展示数据条数发生变化时
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    //添加SPU
    addSpu() {
      this.scene = 1;
      //让子组件调用方法
      this.$refs.spu.addSpuData(this.category3Id);
    },
    //修改SPU
    updataSpu(row) {
      this.scene = 1;
      //调用子组件的方法
      this.$refs.spu.initSpuData(row);
    },
    //改变scene的回调
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag == "修改") {
        //若是修改再还是停在当前页
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    //删除SPU
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    //查看SKU
    async checkSku(spu) {
      this.dialogTableVisible = true;
      //保存spu的信息
      this.spu = spu;
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        //成功之后把loading隐藏
        this.loading = false;
      }
    },
    //添加SKU
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getSkuData(this.category1Id, this.category2Id, row);
    },
    //关闭查看sku的对话框
    closeSku(done) {
      //让loading效果再次变为真
      this.loading = true;
      //清除sku列表的数据
      this.skuList = [];
      //管理对话框，关闭
      done();
    },
  },
};
</script>

<style>
</style>